package com.netsdk.lht2.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.netsdk.demo.util.sql.Trajectory;
import com.netsdk.lht2.push.Coordinates;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class TrajectoryUtil {
    private static Random random = new Random();
//    private static int index = 33000000;
    private static int index = 37150000;
    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static String toPushString(Trajectory trajectory){
        if(StrUtil.isBlank(trajectory.getTime())){
            return "";
        }
        Map<String,Object> map = new HashMap<>();
        map.put("name",trajectory.getName());
        map.put("time",trajectory.getTime());
        map.put("lon",Double.parseDouble(trajectory.getLon()));
        map.put("sog",Double.parseDouble(trajectory.getSog()));
        map.put("cog",Double.parseDouble(trajectory.getCog()));
        map.put("heading",Double.parseDouble(trajectory.getHeading()));
        map.put("mmsi",trajectory.getMmsi());
        map.put("imo",Integer.parseInt(trajectory.getImo()));
        map.put("callsign",trajectory.getCallsign());
        map.put("length",Double.parseDouble(trajectory.getLength()));
        map.put("shipTypeName",trajectory.getShipTypeName());
        map.put("timeout",Integer.parseInt(trajectory.getTimeout()));
        map.put("inlandShipMark",false);
        map.put("width",Double.parseDouble(trajectory.getWidth()));
        map.put("shipTypeClassName",trajectory.getShipTypeClassName());
        map.put("lat",Double.parseDouble(trajectory.getLat()));
        return JSON.toJSONString(map);
    }

    public static String toPushStringArr(Trajectory[] trajectoryArr){
        StringBuilder sb = new StringBuilder();
        for (Trajectory trajectory : trajectoryArr) {
            sb.append(toPushString(trajectory));
        }
        // sb.append("\n");
        return sb.toString();
    }

    public static String generateUuid(){
        // 生成一个范围在 [10000, 99999] 之间的随机数
//        int min = 1000000;
//        int max = 9999999;
//        int randomFiveDigitNumber = random.nextInt((max - min) + 1) + min;
        index ++;
        System.out.println("当前index："+index);
        return  index+ "";
    }

    public static String toPushStringList(List<Trajectory[]> list) {
        StringBuilder sb = new StringBuilder();
        for (Trajectory[] trajectories : list) {
            String bpdy = toPushStringArr(trajectories);
            sb.append(bpdy);
        }
        return sb.toString();
    }

    public static   List<Trajectory[]> create(String utc,int size,String channel,String index){
        List<Trajectory[]> data = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            String uuid = generateUuid();
            String cog = Coordinates.getCog(channel);
            List<Coordinates.Coordinate> coordinates = Coordinates.getCoordinate(channel+"-"+index);

            Trajectory[] trajectoryArr = createTrajectoryArr(coordinates.size(),cog, utc, uuid);
            for (int i1 = 0; i1 < trajectoryArr.length; i1++) {
                Coordinates.Coordinate coordinate = coordinates.get(i1);
                trajectoryArr[i1].setLon(coordinate.getLon());
                trajectoryArr[i1].setLat(coordinate.getLat());
            }
            data.add(trajectoryArr);

        }
        return data;
    }



    private static Trajectory[] createTrajectoryArr(int size,String cog, String utc,String mmsi){
        Trajectory[] arr = new Trajectory[size];
        String[] carInfo = getCarInfo();
        String plate = ShanghaiLicensePlateGenerator.generate();
        for (int i = 0; i < size; i++) {
            arr[i] = createTrajectory(cog,utc,mmsi,carInfo[0],carInfo[1],carInfo[2],carInfo[3]);
            arr[i].setOrderno((i+1)+"");
            arr[i].setName(plate);
        }
//        arr[1] = createTrajectory(cog,utc,mmsi,carInfo[0],carInfo[1],carInfo[2],carInfo[3]);
//        arr[1].setOrderno("2");
//        arr[2] = createTrajectory(cog,utc,mmsi,carInfo[0],carInfo[1],carInfo[2],carInfo[3]);
//        arr[2].setOrderno("3");
//        arr[3] = createTrajectory(cog,utc,mmsi,carInfo[0],carInfo[1],carInfo[2],carInfo[3]);
//        arr[3].setOrderno("4");
//        arr[4] = createTrajectory(cog,utc,mmsi,carInfo[0],carInfo[1],carInfo[2],carInfo[3]);
//        arr[4].setOrderno("5");
//        arr[5] = createTrajectory(cog,utc,mmsi,carInfo[0],carInfo[1],carInfo[2],carInfo[3]);
//        arr[5].setOrderno("6");
//        arr[6] = createTrajectory(cog,utc,mmsi,carInfo[0],carInfo[1],carInfo[2],carInfo[3]);
//        arr[6].setOrderno("7");
        return arr;
    }

    /**
     *  创建车辆轨迹实体类
     */
    private static Trajectory createTrajectory(String cog,String utc,String mmsi,
                                        String shipTypeName,String length,String width,String shipTypeClassName){
        Trajectory trajectory = new Trajectory();
        trajectory.setCog(cog);
        trajectory.setUtc(utc);
        trajectory.setMmsi(mmsi);
        trajectory.setShipTypeName(shipTypeName);
        trajectory.setLength(length);
        trajectory.setWidth(width);
        trajectory.setShipTypeClassName(shipTypeClassName);

        // 设置固定参数
        trajectory.setState("0");
        //trajectory.setName("tt");
        trajectory.setSog("10");
        trajectory.setTimeout("100");
        trajectory.setHeading("0");
        trajectory.setCallsign("0");
        trajectory.setInlandShipMark("false");
        trajectory.setImo("0");
        return trajectory;
    }

    /*
     * 随机获取车辆类型信息
     * */
    private static String[] getCarInfo(){
        int randomValue = random.nextInt(100);
        String[] carInfo = new String[]{"私家车","4", "2","小轿车"};
        if (randomValue>=90) {
            carInfo[0] = "面包车";
            carInfo[1] = "6";
            carInfo[2] = "3";
            carInfo[3] = "中型车";
        } else if (randomValue>80 ) {
            carInfo[0] = "公交车";
            carInfo[1] = "10";
            carInfo[2] = "3.5";
            carInfo[3] = "大型车";
        } else if (randomValue > 50 && randomValue<70) {
            carInfo[0] = "出租车";
            carInfo[1] = "4";
            carInfo[2] = "2";
            carInfo[3] = "小轿车";
        } else if (randomValue > 70) {
            carInfo[0] = "大货车";
            carInfo[1] = "10";
            carInfo[2] = "3.5";
            carInfo[3] = "大型车";
        }
        return carInfo;
    }

    public static void setTime(Trajectory[] data, LocalDateTime startTime){
        LocalDateTime currentTime = startTime;
        for (Trajectory datum : data) {
            datum.setTime(currentTime.format(dateTimeFormatter));
            currentTime = currentTime.plusSeconds(1);
        }
    }
}

